<?php
   
/**
 * class string
 * 
 * @author CTB
 * @since 8/11/2013
*/

class string{
   
   /**
    * Default contrucstor
    * 
    * @param string
   */
   public function __construct($string = null){
   }
   
   /**
    * Remove HTMT to Text
    * 
    * @param String HTML
    * @param Tags allow
   */
   function removeHTML($string,$allowTags = null){
      if(!$this->isHTML($string)){
         return $string;
      }else{
         if($allowTags != null){
            $string = strip_tags($string,$allowTags);
         }else{
            $string = strip_tags($string);
         }
         return $string;
      }
   }
   
   /**
    * Calculate the similarity between two strings
    * 
    * @param String first
    * @param String seconds
    * @return int percen similar bettewn two strings
   */
   function similarText($fistString,$secondString){
      $fistString    = trim($fistString);
      $secondString  = trim($secondString);
      if($fistString == '' && $secondString == ''){
         return 0;
      }
      if(($fistString != '' && $secondString == '') || ($fistString == '' && $secondString != '')){
         return 0;
      }else{
         similar_text($fistString,$secondString,$percen);
         return number_format($percen,0);
      }
   }
   
   /**
    * Calculate the similarity between two keyword search
    * 
    * @param String first
    * @param String seconds
    * @return int percen similar bettewn two keyword search
   */
   function similarKeyword($fistKeyword,$secondKeyword){
      $fistKeyword    = $this->convertUTF8($fistKeyword);
      $fistKeyword    = $this->removeWhitespace($fistKeyword);
      $fistKeyword    = strtolower($fistKeyword);  
      $secondKeyword  = $this->convertUTF8($secondKeyword);
      $secondKeyword  = $this->removeWhitespace($secondKeyword);
      $secondKeyword  = strtolower($secondKeyword);
      if($fistKeyword == '' && $secondKeyword == ''){
         return 0;
      }
      if(($fistKeyword != '' && $secondKeyword == '') || ($fistKeyword == '' && $secondKeyword != '')){
         return 0;
      }else{
         similar_text($fistKeyword,$secondKeyword,$percen);
         return number_format($percen,0);
      }
   }
   
   /**
    * Convert UTF-8 to ASCII
    * 
    * @param String utf-8
    * @return String ascii
    * 
   */
   function convertUTF8($str) {
   	if(!$str) return false;
   	$utf8 = array(
               'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ|Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
               'd'=>'đ|Đ',
               'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ|É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
               'i'=>'í|ì|ỉ|ĩ|ị|Í|Ì|Ỉ|Ĩ|Ị',
               'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ|Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
               'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự|Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
               'y'=>'ý|ỳ|ỷ|ỹ|ỵ|Ý|Ỳ|Ỷ|Ỹ|Ỵ',
   			);
   	foreach($utf8 as $ascii=>$uni) $str = preg_replace("/($uni)/i",$ascii,$str);
      return $str;
   }
   
   /**
    * 
    * Remove whitespace string
    * @param String
    * @return String not contain whitespace
   */
   function removeWhitespace($string){
      if(!$string)
         return false;
      else{
         $string = trim($string);
         $string = str_replace(' ','',$string);
         return $string;
      }
   }
   
   /**
    * Remove MySql Injection
    * 
    * @param string
    * @return string
   */
   function removeInjection($string){
      if(get_magic_quotes_gpc()){
         return mysql_real_escape_string(stripslashes($string));
      }else{
         return mysql_real_escape_string($string);
      } 
   }
   
   
   /**
    * Check is HTML
    * 
    * @param string
    * @return bool. If is html return true else return false
   */
   
   function isHTML($string){
      return preg_match("/<[^<]+>/",$string) != 0;
   }
}
   
?>